package core

import (
	"context"
	"log/slog"

	"github.com/redis/go-redis/v9"
	"github.com/spf13/viper"
)

var (
	REDIS redisClient
)

// redis操作对象
type redisClient struct {
	*redis.Client
}

func (r *redisClient) dc() {
	viper.SetDefault("redis.addr", "127.0.0.1:6379")
	viper.SetDefault("redis.password", "miot")
	viper.SetDefault("redis.db", 0)
}

// 配置redis连接
func (r *redisClient) setup() {
	addr := viper.GetString("redis.addr")
	password := viper.GetString("redis.password")
	dbi := viper.GetInt("redis.db")
	client := redis.NewClient(&redis.Options{
		Network:  "tcp",
		Addr:     addr,
		Password: password,
		DB:       dbi,

		PoolSize:     15,
		MinIdleConns: 2,
	})
	err := client.Ping(context.Background()).Err()
	if err != nil {
		panic(err)
	}

	r.Client = client
	slog.Info("连接到redis服务器")
}

func (r *redisClient) exit() {
	r.Client.Close()
}
